//
//  SearchViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "aStageHeader.pch"
@interface SearchViewController ()<UISearchBarDelegate,UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSString *inputStr;//搜索框输入内容
@property (nonatomic, retain)NSMutableArray *dataArr;
@end

@implementation SearchViewController

- (void)dealloc
{   [_dataArr release];
    [_tableView release];
    [_inputStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self creatSearchBar];//创建searchBar
    [self creatTableView];//创建tableView
    self.inputStr = @"";
    //初始化数组
    self.dataArr = [NSMutableArray array];
    [self dataHandle];//数据处理
    
}
#pragma mark - 数据处理
- (void)dataHandle
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?c=query&a=index&q=%@&type=shop&vid=15", self.inputStr] completion:^(id result) {
        self.dataArr = [SearchModel modelHandlerWithArray:result[@"data"][@"hits"]];
        [_tableView reloadData];
    }];
}
#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, WIDTH, HEIGHT - 80) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    //注册重用池
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count  ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
#pragma mark - cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SearchModel *m = _dataArr[indexPath.row];
#pragma mark - 高亮设置
    NSMutableAttributedString *atemStr = [[NSMutableAttributedString alloc] initWithString:m._source[@"title"]];
    NSRange range = [m._source[@"title"] rangeOfString:_inputStr];
    [atemStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName: [UIFont systemFontOfSize:18]} range:range];
    cell.textLabel.attributedText = atemStr;
    return cell;
}


#pragma mark - 创建searchBar
- (void)creatSearchBar
{
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    searchBar.showsCancelButton = YES;
    searchBar.delegate = self;
    searchBar.placeholder = @"搜索";
    [self.view addSubview:searchBar];
    [searchBar release];
}
//取消返回上一个页面
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark - 协议方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    //获取输入的字符
    self.inputStr = searchText;
    
    [self dataHandle];
}
//点击搜索触发
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"%@", self.inputStr);
    [searchBar resignFirstResponder];
    [self dataHandle];//加载
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
